package cc.eddic.practice.concurrency;

import cc.eddic.practice.concurrency.impl.LogParserExecutorImpl;
import cc.eddic.practice.concurrency.impl.LogParserFutureImpl;
import cc.eddic.practice.concurrency.impl.LogParserImpl;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@SpringBootApplication
public class ConcurrencyHrjApplication implements CommandLineRunner {
    public static void main(String[] args) {
        SpringApplication.run(ConcurrencyHrjApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.debug("--ok--");

        long start = System.currentTimeMillis();
        log.info("start");
        //loop();
        executor();
        log.info("elapsed {} ms", System.currentTimeMillis() - start);
    }

    private void executor() {
        ExecutorService executorService = Executors.newFixedThreadPool(8);
        //LogParserExecutorImpl logParserExecutor = new LogParserExecutorImpl();

        LogParserFutureImpl logParserExecutor = new LogParserFutureImpl();

        logParserExecutor.parse("D:\\\\nginx-logs");

        //CountDownLatch latch = new CountDownLatch(LOG_FILE_COUNT);




//        for (int i = 0; i < LOG_FILE_COUNT; i++) {
//            int finalI = i;
//            executorService.submit(() -> dummyParse(finalI, latch));
//        }



//        try {
//            latch.await();
//        } catch (InterruptedException e) {
//            log.error(e.getMessage());
//        }
//
//        ExecutorServiceUtil.shutdown(executorService);

    }

    //方法的重载，方法名一样，参数不同
    private void dummyParse(int i, CountDownLatch latch) {
        dummyParse(i);
        //CountDownLatch的countDown方法，对计数器进行递减1操作
        latch.countDown();
    }

    private void loop() {
        for (int i = 0; i < LOG_FILE_COUNT; i++) {
            dummyParse(i);
        }
    }


    private void dummyParse(int i) {
        try {
            //val r = rnd(10, 50);
            val r = 10;
            if (log.isDebugEnabled()) log.debug("{} => sleep {} ms", i, r);
            Thread.sleep(r);
        } catch (InterruptedException e) {
            log.error(e.getMessage());
        }

    }

    private int rnd(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }

    private final int LOG_FILE_COUNT = 256;

    private final Random random = new Random(System.currentTimeMillis());


    private static void parser() {
        long startTime = System.currentTimeMillis();

        LogParserImpl logParser = new LogParserImpl();

        Map<String, List<LogItem>> logItems = logParser.parse("D:\\\\nginx-logs");
        int num = 0;
        for (String s : logItems.keySet()) {
            num = num + logItems.get(s).size();
        }

        long time = System.currentTimeMillis() - startTime;
        if (log.isInfoEnabled()) {
            log.info("elapsed {}ms", time);
            log.info("total log items : {}", num);
            log.info("valid log items : {}", logItems.values().stream().flatMap(Collection::stream).filter(LogItem::isValidRequest).count());
        }
    }

}
